//
//  DrawLineCell.m
//  UITest
//
//  Created by user on 2017/5/8.
//  Copyright © 2017年 xbs. All rights reserved.
//

#import "XBSDrawLineCell.h"
#import "MLWaterWaveView.h"

@interface XBSDrawLineCell()

@property(nonatomic,strong)NSArray *titles;

@property(nonatomic,strong)NSMutableArray *labels;

@property(nonatomic,strong)NSMutableArray *grayCornerViews;

@property(nonatomic,strong)NSMutableArray *orangeConerViews;

@property(nonatomic,weak)UIView *grayHorLine;

@property(nonatomic,weak)UIView *orangeHorLine;

@property(nonatomic,weak)MLWaterWaveView *waterWaveView;

@end

@implementation XBSDrawLineCell

- (void)awakeFromNib {
    [super awakeFromNib];
   
    [self setupUI];
    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        [self setupUI];
    }
    
    return self;
}
- (void)setupUI{
    
    self.backgroundColor = [UIColor whiteColor];
    
    self.titles = @[@"提交订单",@"派单",@"接单",@"到达"];
    self.labels = @[].mutableCopy;
    self.grayCornerViews = @[].mutableCopy;
    self.orangeConerViews = @[].mutableCopy;
    self.billStatus = self.titles[1];

    for (NSInteger i = 0 ; i < self.titles.count; i++) {
        UIView *grayView = [UIView new];
        grayView.backgroundColor = [UIColor colorWithHexString:@"#d8d8d8"];
        grayView.layer.masksToBounds =  YES;
        [self.contentView addSubview:grayView];
        
        [self.grayCornerViews addObject:grayView];
        
    }
    for (NSInteger i = 0; i < self.titles.count; i++) {
        
        UILabel *label = [UILabel new];
        label.text = self.titles[i];
        label.textColor = [UIColor colorWithHexString:@"#666666"];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:label];
        [label sizeToFit];
        
        [self.labels addObject:label];
    }

    for (NSInteger i = 0; i < self.titles.count - 1; i++) {
        
        UIView *orangeView = [UIView new];
        orangeView.backgroundColor = [UIColor colorWithHexString:@"#fe880e"];
        orangeView.layer.masksToBounds =  YES;
        [self.contentView addSubview:orangeView];
        
        [self.orangeConerViews addObject:orangeView];

    }
    
}
- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    CGFloat startX = 43;
    CGFloat grayW = 7;
    CGFloat grayH = grayW;
    CGFloat space = (self.width - 4 * grayW - 2 * startX) / 3;
    CGFloat startCenterX = 0;
    NSInteger index = [self.titles indexOfObject:self.billStatus];

    for (NSInteger i = 0; i < self.grayCornerViews.count; i++) {
        
        //处理灰色圆点
        UIView *grayView = self.grayCornerViews[i];
        grayView.backgroundColor = [UIColor colorWithHexString:@"#d8d8d8"];
        grayView.size = CGSizeMake(grayW, grayH);
        grayView.layer.cornerRadius =  grayView.size.width * 0.5;
        grayView.alpha = 1;
        [self addSubview:grayView];
        
        grayView.left = startX + i*(space + grayView.width);
        grayView.centerY = self.height - 15;
        
        //处理灰色线条
        if (i == self.grayCornerViews.count - 1) {
         
            self.grayHorLine.centerY = grayView.centerY;
            self.grayHorLine.left = startX;
            self.grayHorLine.width = grayView.centerX - startX;
            
        }
        
        //处理顶部标签
        UILabel *label = self.labels[i];
        label.bottom = grayView.top - 10;
        label.centerX = grayView.centerX;
        
        //处理橘色线和点
        if (i == 0) {
            
            startCenterX = grayView.centerX;

            self.orangeHorLine.centerY = grayView.centerY ;
            self.orangeHorLine.left = startCenterX;
        }
        if (i == index) {
            
            self.waterWaveView.center = grayView.center;
            self.orangeHorLine.width = grayView.centerX - startCenterX;
            grayView.alpha = 0;

        }
        else if(i < index){
            
            UIView *orangeView = self.orangeConerViews[i];
            orangeView.frame = grayView.frame;
            orangeView.layer.cornerRadius =  orangeView.size.width * 0.5;
            grayView.alpha = 0;
        }
    }
    
}
- (void)setBillStatus:(NSString *)billStatus{
    
    _billStatus = [billStatus copy];
    
    [self setNeedsLayout];
    
}

- (UIView *)grayHorLine{
    
    if (!_grayHorLine) {
        
        UIView *grayHorLine = [UIView new];
        grayHorLine.backgroundColor = [UIColor colorWithHexString:@"#d8d8d8"];
        grayHorLine.height = 1;
        [self.contentView addSubview:grayHorLine];
        [self.contentView sendSubviewToBack:grayHorLine];
        _grayHorLine = grayHorLine;
    }
    return _grayHorLine;
}

- (UIView *)orangeHorLine{
    
    if (!_orangeHorLine) {
        UIView *orangeHorLine = [UIView new];
        orangeHorLine.backgroundColor = [UIColor colorWithHexString:@"#fe880e"];
        orangeHorLine.height = 1;
        [self.contentView addSubview:orangeHorLine];
        _orangeHorLine = orangeHorLine;
    }
    return _orangeHorLine;
}

- (MLWaterWaveView *)waterWaveView{
    
    if (!_waterWaveView) {
        
        MLWaterWaveView *waterWaveView = [[MLWaterWaveView alloc] init];
        waterWaveView.backgroundColor = [UIColor colorWithHexString:@"#fe880e"];
        waterWaveView.borderWidth = 0;
        waterWaveView.fillColor =  waterWaveView.backgroundColor;
        waterWaveView.fromValue = 0.45;
        waterWaveView.width = 11;
        waterWaveView.height = waterWaveView.width;
        [self.contentView addSubview:waterWaveView];
        _waterWaveView = waterWaveView;
    }
    
    return _waterWaveView;
}



@end
